package model;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * class to represent a ID3Tag of a mp3 file
 * contains the header as byte array, the size saved as integer and two hash maps with the supported and unsupported frames 
 * 
 *
 */
public class ID3Tag {

	/*****************************
	 * MEMBER
	 *****************************/
	private byte[] header;
	private int size;
	private HashMap<String, Frame> usedFrames;
	private HashMap<String, Frame> otherFrames;
	
	/*****************************
	 * CONSTRUCTOR
	 *****************************/
	public ID3Tag(byte[] header, int size) {
		this.header = header;
		this.size = size;
		this.usedFrames = new HashMap<String, Frame>();
		this.otherFrames = new HashMap<String, Frame>();
	}
	
	public ID3Tag() {
		this.header = new byte[1];
		this.size = 0;
		this.usedFrames = new HashMap<String, Frame>();
		this.otherFrames = new HashMap<String, Frame>();
	}

	public void setUsedFrames(HashMap<String, Frame> usedFrames) {
		this.usedFrames = usedFrames;
	}
	public void setOtherFrames(HashMap<String, Frame> otherFrames) {
		this.otherFrames = otherFrames;
	}
	/*****************************
	 * METHODS
	 *****************************/
	public byte[] getHeader() {
		return header;
	}

	public void setHeader(byte[] header) {
		this.header = header;
	}
	
	/**
	 * return size as saved in header
	 * @return size of id3 tag without headersize
	 */
	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public ArrayList<Frame> getListOfUsedFrames() {
		return new ArrayList<Frame>(usedFrames.values());
	}
	
	public ArrayList<Frame> getListOfOtherFrames() {
		return new ArrayList<Frame>(otherFrames.values());
	}
	public HashMap<String, Frame> getUsedFrames() {
		return usedFrames;
	}
	public HashMap<String, Frame> getOtherFrames() {
		return otherFrames;
	}
	public Frame getFrame(String key) {
		return usedFrames.get(key);
	}
	
	public void addUsedFrame(String key, Frame frame) {
		usedFrames.put(key, frame);
	}
	
	public void addOtherFrame(String key, Frame frame) {
		otherFrames.put(key, frame);
	}
	
	/**
	 * calculate new size of ID3Tag (without header size)
	 */
	public void resize() {
		this.size = 0;
		for(Frame f : usedFrames.values()) {
			this.size += f.getSize();
		}
		for(Frame f : otherFrames.values()) {
			this.size += f.getSize();
		}

	}
	
	
}
